package spring.cloud.tasks.tasks_assign_manager.domain;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import spring.cloud.tasks.common.zookeeper.ZooKeeper;
import spring.cloud.tasks.tasks_assign_manager.BootStrapConnectionStateListener;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Setter
@Getter
public class ZooKeeperCluster {


    private String zooKeeperClusterKey;

    private String alias;

    private String zooKeeperAddressList;

    private String description;

    private String digest;

    private boolean offline = false;


    private transient ZooKeeper zooKeeper;


    private transient BootStrapConnectionStateListener connectionStateListener;


    private List<NameSpace> nameSpaceList = new ArrayList<NameSpace>();
    private String tasksManagerId;

    public ZooKeeperCluster() {
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof ZooKeeperCluster)) {
            return false;
        }

        ZooKeeperCluster zooKeeperCluster = (ZooKeeperCluster) o;

        if (zooKeeperClusterKey != null ? !zooKeeperClusterKey.equals(zooKeeperCluster.zooKeeperClusterKey) : zooKeeperCluster.zooKeeperClusterKey != null) {
            return false;
        }
        if (alias != null ? !alias.equals(zooKeeperCluster.alias) : zooKeeperCluster.alias != null) {
            return false;
        }
        if (zooKeeperAddressList != null ? !zooKeeperAddressList.equals(zooKeeperCluster.zooKeeperAddressList) : zooKeeperCluster.zooKeeperAddressList != null) {
            return false;
        }
        if (description != null ? !description.equals(zooKeeperCluster.description) : zooKeeperCluster.description != null) {
            return false;
        }
        return digest != null ? digest.equals(zooKeeperCluster.digest) : zooKeeperCluster.digest == null;

    }


    /**
     * 某些信息相等时不需要更新
     */
    public boolean equalsNoNeedReconnect(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof ZooKeeperCluster)) {
            return false;
        }

        ZooKeeperCluster zooKeeperCluster = (ZooKeeperCluster) o;

        if (zooKeeperClusterKey != null ? !zooKeeperClusterKey.equals(zooKeeperCluster.zooKeeperClusterKey) : zooKeeperCluster.zooKeeperClusterKey != null) {
            return false;
        }
        if (alias != null ? !alias.equals(zooKeeperCluster.alias) : zooKeeperCluster.alias != null) {
            return false;
        }
        if (zooKeeperAddressList != null ? !zooKeeperAddressList.equals(zooKeeperCluster.zooKeeperAddressList) : zooKeeperCluster.zooKeeperAddressList != null) {
            return false;
        }
        return digest != null ? digest.equals(zooKeeperCluster.digest) : zooKeeperCluster.digest == null;

    }

    @Override
    public int hashCode() {
        int result = zooKeeperClusterKey != null ? zooKeeperClusterKey.hashCode() : 0;
        result = 31 * result + (alias != null ? alias.hashCode() : 0);
        result = 31 * result + (zooKeeperAddressList != null ? zooKeeperAddressList.hashCode() : 0);
        result = 31 * result + (description != null ? description.hashCode() : 0);
        result = 31 * result + (digest != null ? digest.hashCode() : 0);
        return result;
    }
}
